
import {
  http
} from '@/api/service.js'
import Vue from 'vue'

const wsconnect = {
	requestJson(params,reqTagName){
		const form = {
			...params,
			token:Vue.prototype.token
		}
		console.log("webservice params:" + JSON.stringify(form) +',reqTagName' + reqTagName)
		return new Promise((resolve,reject) => {
			http.post('/realhistorian/requestJson',form).then(res => {
				if(res.data.data.code == 0){//设置成功
					uni.showLoading({
						mask:true,
					})
					setTimeout(()=>{this.getValueWithTagAction(1,reqTagName,resolve,reject)},1000);
				}else{
					reject({code:0,msg:'出错了，请稍后再试,err:'+res.data.data.message})
				}
			}).catch(err => {
				reject({code:0,msg:'出错了，请稍后再试'})
			})		
		})
	},
	getValueWithTagAction(count,reqTagName,resolve,reject){//获取基站信息
		if(count > 10){
			uni.hideLoading()
			return reject({code:0,msg:'重启次数过多，请稍后再试'})
		}
		const form = {
			items:[reqTagName+'.DESC'],
			type:"GetDataByName",
			token:Vue.prototype.token
		}
		http.post('/realhistorian/requestJson',form).then(res => {
			clearTimeout();
			console.log(res.data.data);
			if(res.data.data.code == 0){//获取成功
				const bsInqRp = res.data.data.items[0];
				if(bsInqRp.length > 0 ){
					return resolve(bsInqRp)
				}else{
					setTimeout(()=>{this.getValueWithTagAction(++count,reqTagName,resolve,reject)},1000);
				}
			}
			else if (res.data.data.code == 3){//token失效
				uni.hideLoading()
				return reject({code:3,msg:'token失效'})
				
			}else{
				uni.hideLoading()
				return reject({code:0,msg:'出错了，请稍后再试,err:'+res.data.data.message})
			}
		}).catch(err => {
			uni.hideLoading()
			return reject({code:0,msg:'出错了，请稍后再试'})
		})
	},
	//清除接收点数据，用于校验是否接收到数据
	clearReqData(count,reqTagName){
		if(count > 20){
			uni.hideLoading()
		}
		const form = {
			items:[reqTagName+'.DESC'],
			vals:[''],
			type:"SetDataByName",
			token:Vue.prototype.token
		}
		console.log("clear webservice params:" + JSON.stringify(form)+',reqTagName' + reqTagName)
		http.post('/realhistorian/requestJson',form).then(res => {
			if(res.data.data.code == 0){//设置成功
				uni.hideLoading()						
			}else{
				this.clearReqData(++count)
			}
		}).catch(err => {
			this.clearReqData(++count)
		})
	}
}
export default wsconnect